package actions;

import java.sql.SQLException;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.Applicant;
import model.Database;
import model.Degree;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import db.DbException;
import forms.ResidenceSubmitForm;

public class ResidenceSubmitAction extends Action {
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws DbException {
		HttpSession session = request.getSession();
		Applicant applicant = (Applicant)session.getAttribute("applicant");
   		
		// Cast the form
		ResidenceSubmitForm iForm = (ResidenceSubmitForm) form;
		String country = iForm.getCountry();
		System.out.println(country);
		applicant.setCountry_of_residence(country);
		Vector<String> statesVector = null;
		Degree degree = new Degree();
		
		try {
			 statesVector = Database.getStates();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		if(country.equals("United States")){
			applicant.setDomestic(true);
			
		}else{
			applicant.setDomestic(false);
		}
		session.setAttribute("statesVector", statesVector);
		session.setAttribute("applicant", applicant);
		session.setAttribute("degree", degree);

   		return mapping.findForward("success");
	}
}